import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.datasets import Planetoid
from torch_geometric.datasets import TUDataset
import networkx as nx
import torch_geometric
import matplotlib.pyplot as plt
from torch_geometric.data import DataLoader
import torch_geometric.nn as pyg_nn
import numpy as np
import os
os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'


# 加载Cora数据集.(自动帮你下载)
# dataset = TUDataset(root='/temp/ENZYMES', name='ENZYMES')
import torch
from torch_geometric.data import Data

edge_index = torch.tensor([[0, 1, 1, 2],
                           [1, 0, 2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)
y = torch.tensor([5], dtype=torch.float)
dataset = Data(x=x,y=y, edge_index=edge_index)


print(dataset.num_features)
print(dataset.num_classes)
print(len(dataset))
print(dataset)
data = dataset[0]
print(data)
# edge_index代表的是边矩阵，是2行，74564列。x是特征矩阵，19580行，3列，说明有19580个node，y是标签。
# Node feature matrix with shape [num_nodes, num_node_features]
print(data.x)
print(data.x.shape)
# Graph connectivity in COO format with shape [2, num_edges] and type torch.long
print(data.edge_index)
# Edge feature matrix with shape [num_edges, num_edge_features]
# 这个是边的属性矩阵，类比点的属性矩阵
print(data.edge_attr)
# Target to train against (may have arbitrary shape),
# e.g., node-level targets of shape [num_nodes, *] or graph-level targets of shape [1, *]
print(data.y)
print(len(data.y))
# Node position matrix with shape [num_nodes, num_dimensions]
print(data.pos)
edge,x,y = data  # 得到edge，node矩阵和y即label
print(edge,x,y)
numpyx = x[1].numpy()
numpyy = y[1].numpy()
numpyedge = edge[1].numpy()
# 将整个数据集可视化出来
g = nx.DiGraph() # 建一个空的有向图
name,edgeinfo = edge
src = edgeinfo[0].numpy()
dst = edgeinfo[1].numpy()
edgelist = zip(src,dst)
for i,j in edgelist:
    g.add_edge(i,j)
plt.rcParams['figure.dpi'] = 300 #分辨率
fig , ax1 = plt.subplots(figsize=(9,9))
nx.draw_networkx(g , ax = ax1 , font_size=12 , node_size = 350)
plt.show()
print(len(g.nodes))
print(g.nodes)





